package com.lm.jsoup.example;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class JsoupExtractLinks {
    public static void main(String[] args) {
        String html = "<html><body>" +
                "<a href='https://example.com'>Example</a>" +
                "<img src='image.jpg' alt='Example Image'>" +
                "</body></html>";

        // 解析 HTML
        Document document = Jsoup.parse(html);

        // 提取链接
        Elements links = document.select("a[href]");
        for (Element link : links) {
            System.out.println("Link: " + link.attr("href") + " Text: " + link.text());
        }

        // 提取图片
        Elements images = document.select("img[src]");
        for (Element image : images) {
            System.out.println("Image: " + image.attr("src") + " Alt: " + image.attr("alt"));
        }
    }
}
